package com.ruoyi.equ.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.uuid.IdUtils;

import com.ruoyi.equ.domain.EquDetailsType;
import com.ruoyi.equ.service.IEquDetailsTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 设备明细类型信息Controller
 * 
 * @author lgq
 * @date 2023-02-21
 */
@RestController
@RequestMapping("/equ/type")
public class EquDetailsTypeController extends BaseController
{
    @Autowired
    private IEquDetailsTypeService equDetailsTypeService;

    /**
     * 查询设备明细类型信息列表
     */
    @GetMapping("/list")
    public TableDataInfo list(EquDetailsType equDetailsType)
    {
        startPage();
        List<EquDetailsType> list = equDetailsTypeService.selectEquDetailsTypeList(equDetailsType);
        return getDataTable(list);
    }

    /**
     * 导出设备明细类型信息列表
     */
    @Log(title = "设备明细类型信息", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(EquDetailsType equDetailsType)
    {
        List<EquDetailsType> list = equDetailsTypeService.selectEquDetailsTypeList(equDetailsType);
        ExcelUtil<EquDetailsType> util = new ExcelUtil<EquDetailsType>(EquDetailsType.class);
        return util.exportExcel(list, "type");
    }

    /**
     * 获取设备明细类型信息详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return AjaxResult.success(equDetailsTypeService.selectEquDetailsTypeById(id));
    }

    /**
     * 新增设备明细类型信息
     */
    @Log(title = "设备明细类型信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody EquDetailsType equDetailsType)
    {
        equDetailsType.setId(IdUtils.fastSimpleUUID());
        return toAjax(equDetailsTypeService.insertEquDetailsType(equDetailsType));
    }

    /**
     * 批量新增设备明细类型信息
     */
    @Log(title = "设备明细类型信息", businessType = BusinessType.INSERT)
    @PostMapping("/batchAdd")
    public AjaxResult batchAdd(@RequestBody List<EquDetailsType> equDetailsTypes)
    {
        int addRes = 0;
        if(equDetailsTypes.size() > 0){
            //删除原本数据
            int res = equDetailsTypeService.deleteEquDetailsTypeByEquId(equDetailsTypes.get(0).getEquId());
            if(res >= 0){
                //新增数据
                for(EquDetailsType equs: equDetailsTypes){
                    equs.setId(IdUtils.fastSimpleUUID());
                    equs.setUploadtime(DateUtils.getNowDate());
                    equs.setUploader(SecurityUtils.getLoginUser().getUser().getNickName());
                }
                addRes = equDetailsTypeService.insertEquDetails(equDetailsTypes);
            }
        }
        return toAjax(addRes);
    }

    /**
     * 修改设备明细类型信息
     */
    @Log(title = "设备明细类型信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody EquDetailsType equDetailsType)
    {
        return toAjax(equDetailsTypeService.updateEquDetailsType(equDetailsType));
    }

    /**
     * 删除设备明细类型信息
     */
    @Log(title = "设备明细类型信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(equDetailsTypeService.deleteEquDetailsTypeByIds(ids));
    }
}
